import { Component, ChangeDetectorRef } from "@angular/core";
import { SliderLevelModel } from "../../../../../../../shared/components/slider/sliderLevel.model";
import { BaseCriteriaTemplateComponent } from "../baseType.component";
import { ReportingService } from "../../../../../reporting.service";
import { Subscription } from "rxjs";
import { ICriteriaTemplate, TemplateType } from "../../models/criteriaTemplate.interface";
import { ICriteriaOutput } from "../../models/save/criteriaOutput.interface";
import { SliderOutput } from "../../models/save/criteriaOutput.model";

@Component({
    selector: 'type8',
    templateUrl: 'type8.component.html',
    styleUrls: ['../../templateCriteria.component.scss'],
    providers: [{ provide: BaseCriteriaTemplateComponent, useExisting: Type8Component }]
})
export class Type8Component extends BaseCriteriaTemplateComponent implements ICriteriaTemplate {
    private sub: Subscription;
    public selectedDesc: string;
    public selectedId: number;

    public type: TemplateType = TemplateType.Slider;

    public levels: SliderLevelModel[] = [
        new SliderLevelModel(0, 'Off', ''),
        new SliderLevelModel(1, 'Severe', 'Category 5 involvement is restricted', 'category5'),
        new SliderLevelModel(2, 'High', 'Category 5 and 4 involvement is restricted', 'category4'),
        new SliderLevelModel(3, 'Significant', 'Category 5, 4 and 3 involvement is restricted', 'category3'),
        new SliderLevelModel(4, 'Moderate', 'Category 5, 4, 3, and 2 involvement is restricted', 'category2'),
        new SliderLevelModel(5, 'Low', 'Category 5, 4, 3, 2 and 1 involvement is restricted', 'category1')];

    constructor(
        private toolsService: ReportingService,
        private cdRef: ChangeDetectorRef) {
        super(toolsService, cdRef);
    }

    ngOnInit() {
        this.loadScoreData('typeEightIsScreenableCriteriaItems')
        this.sub = this.load.subscribe(data => {
            this.selected = data || 0;
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
    }

    public set selected(value: number) {
        if (value !== undefined) {
            this.selectedId = value;
            this.selectedDesc = this.levels.find(x => x.id == value).desc;
        }
    }

    public sliderChanged(model) {
        this.selectedId = model.id;
        this.selectedDesc = model.desc;
        BaseCriteriaTemplateComponent.changed.emit(model.id != 0);
    }

    public save(): ICriteriaOutput {
        let model: SliderOutput = new SliderOutput(this.selectedId, undefined);
        model.setBaseClass(this.templateCriteria);
        return model;
    }
}